<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="课程名称"></el-input>
    </el-form-item>
    <el-form-item label="课程状态（0 草稿 1 发布 2 作废 5 其他）" prop="status">
      <el-input v-model="dataForm.status" placeholder="课程状态（0 草稿 1 发布 2 作废 5 其他）"></el-input>
    </el-form-item>
    <el-form-item label="销售价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="销售价格"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="校区id" prop="schoolId">
      <el-input v-model="dataForm.schoolId" placeholder="校区id"></el-input>
    </el-form-item>
    <el-form-item label="教师id" prop="teacherId">
      <el-input v-model="dataForm.teacherId" placeholder="教师id"></el-input>
    </el-form-item>
    <el-form-item label="课程科目id" prop="subjectId">
      <el-input v-model="dataForm.subjectId" placeholder="课程科目id"></el-input>
    </el-form-item>
    <el-form-item label="班级类型id" prop="classTypeId">
      <el-input v-model="dataForm.classTypeId" placeholder="班级类型id"></el-input>
    </el-form-item>
    <el-form-item label="课程分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="课程分类id"></el-input>
    </el-form-item>
    <el-form-item label="年级id" prop="gradeId">
      <el-input v-model="dataForm.gradeId" placeholder="年级id"></el-input>
    </el-form-item>
    <el-form-item label="总课时" prop="lessonNum">
      <el-input v-model="dataForm.lessonNum" placeholder="总课时"></el-input>
    </el-form-item>
    <el-form-item label="剩余名额" prop="restCount">
      <el-input v-model="dataForm.restCount" placeholder="剩余名额"></el-input>
    </el-form-item>
    <el-form-item label="要求分数" prop="score">
      <el-input v-model="dataForm.score" placeholder="要求分数"></el-input>
    </el-form-item>
    <el-form-item label="是否结束（0否1是）" prop="isOver">
      <el-input v-model="dataForm.isOver" placeholder="是否结束（0否1是）"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除 1（true）已删除， 0（false）未删除" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="逻辑删除 1（true）已删除， 0（false）未删除"></el-input>
    </el-form-item>
    <el-form-item label="乐观锁" prop="version">
      <el-input v-model="dataForm.version" placeholder="乐观锁"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          status: '',
          price: '',
          startTime: '',
          endTime: '',
          schoolId: '',
          teacherId: '',
          subjectId: '',
          classTypeId: '',
          categoryId: '',
          gradeId: '',
          lessonNum: '',
          restCount: '',
          score: '',
          isOver: '',
          isDeleted: '',
          version: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          name: [
            { required: true, message: '课程名称不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '课程状态（0 草稿 1 发布 2 作废 5 其他）不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '销售价格不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          schoolId: [
            { required: true, message: '校区id不能为空', trigger: 'blur' }
          ],
          teacherId: [
            { required: true, message: '教师id不能为空', trigger: 'blur' }
          ],
          subjectId: [
            { required: true, message: '课程科目id不能为空', trigger: 'blur' }
          ],
          classTypeId: [
            { required: true, message: '班级类型id不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '课程分类id不能为空', trigger: 'blur' }
          ],
          gradeId: [
            { required: true, message: '年级id不能为空', trigger: 'blur' }
          ],
          lessonNum: [
            { required: true, message: '总课时不能为空', trigger: 'blur' }
          ],
          restCount: [
            { required: true, message: '剩余名额不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '要求分数不能为空', trigger: 'blur' }
          ],
          isOver: [
            { required: true, message: '是否结束（0否1是）不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '逻辑删除 1（true）已删除， 0（false）未删除不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '乐观锁不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/educlass/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.eduClass.name
                this.dataForm.status = data.eduClass.status
                this.dataForm.price = data.eduClass.price
                this.dataForm.startTime = data.eduClass.startTime
                this.dataForm.endTime = data.eduClass.endTime
                this.dataForm.schoolId = data.eduClass.schoolId
                this.dataForm.teacherId = data.eduClass.teacherId
                this.dataForm.subjectId = data.eduClass.subjectId
                this.dataForm.classTypeId = data.eduClass.classTypeId
                this.dataForm.categoryId = data.eduClass.categoryId
                this.dataForm.gradeId = data.eduClass.gradeId
                this.dataForm.lessonNum = data.eduClass.lessonNum
                this.dataForm.restCount = data.eduClass.restCount
                this.dataForm.score = data.eduClass.score
                this.dataForm.isOver = data.eduClass.isOver
                this.dataForm.isDeleted = data.eduClass.isDeleted
                this.dataForm.version = data.eduClass.version
                this.dataForm.gmtCreate = data.eduClass.gmtCreate
                this.dataForm.gmtModified = data.eduClass.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/educlass/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'status': this.dataForm.status,
                'price': this.dataForm.price,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'schoolId': this.dataForm.schoolId,
                'teacherId': this.dataForm.teacherId,
                'subjectId': this.dataForm.subjectId,
                'classTypeId': this.dataForm.classTypeId,
                'categoryId': this.dataForm.categoryId,
                'gradeId': this.dataForm.gradeId,
                'lessonNum': this.dataForm.lessonNum,
                'restCount': this.dataForm.restCount,
                'score': this.dataForm.score,
                'isOver': this.dataForm.isOver,
                'isDeleted': this.dataForm.isDeleted,
                'version': this.dataForm.version,
                // 'gmtCreate': this.dataForm.gmtCreate,
                // 'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
